package cn.ffcs.organ.web;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import cn.ffcs.base.web.BaseAction;
import cn.ffcs.base.web.PageControl;
import cn.ffcs.organ.model.LinkMethod;

/**
 * 联系方式action类<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author liuzhh
 * @version $Id$
 */
public class LinkMethodAction<L extends LinkMethod> extends BaseAction<L>
{

    /** 业务POJO **/
    private L linkMethod;


    /**
     * 新增或修改字典类型数据
     * @return
     */
    public String saveOrUpdate()
    {
        baseService.saveOrUpdate(linkMethod);
        returnMsg="保存操作已成功！";
        return SUCCESS;
    }

    /**
     * 删除数据
     * @return
     */
    public String remove()
    {
        baseService.remove(linkMethod);
        returnMsg="删除操作已成功！";
        return SUCCESS;
    }

    /**
     * 批量删除
     * @return
     */
    public String batchDel()
    {
        String[] ids = getContextObjects("ids");
        baseService.batchDel(ids);
        returnMsg="批量删除操作已成功！";
        return SUCCESS;
    }

    /**
     * 根据关键值获取实体数据
     * @return
     */
    public String findById()
    {
        if (linkMethod != null && linkMethod.getId() != 0)
        {
            linkMethod = (L) baseService.findById(linkMethod.getId());
        }     
        
        return SUCCESS;
    }
    /**
     * 分页查找
     */
    public String findByPage(){
    	if (pageControl == null)
        {
            pageControl = new PageControl();
        }

        pageControl = baseService.findByPage(combineCond(),pageControl.getPageNo(),
                pageControl.getPageSize());

        return SUCCESS;
    }
    /**
     * 组合查询条件
     */
    @SuppressWarnings("unchecked")
	protected Criterion combineCond(){
        Criterion cond = null;
        HashMap<String,Object> conds=super.getConds();
        if (conds != null)
        {
            //遍历参数表
            Iterator i = conds.entrySet().iterator();
            while (i.hasNext())
            {
                Entry entry = (Entry)i.next();
                String key=(String)entry.getKey();
                if (conds.get(key) != null
                        && !StringUtils.isEmpty(conds.get(key).toString()))
                {
                    if (cond == null)
                    {
	                    if(key.indexOf("type")!=-1){
	                		cond = Restrictions.like(key,"'%"+conds.get(key)+"%'");
	                	}else if(key.indexOf("value")!=-1){
	                		cond = Restrictions.like(key,"'%"+conds.get(key)+"%'");
	                	}else{
	                		cond = Restrictions.eq(key, conds.get(key));
	                	}
                    }
                    else
                    {	
                    	if(key.indexOf("type")!=-1){
	                		cond = Restrictions.like(key,"'%"+conds.get(key)+"%'");
	                	}else if(key.indexOf("value")!=-1){
	                		cond = Restrictions.like(key,"'%"+conds.get(key)+"%'");
	                	}else{
	                		cond = Restrictions.eq(key, conds.get(key));
	                	}
                    }
                }
            }
        }
        return cond;
    }
    public L getLinkMethod()
    {
        return linkMethod;
    }

    public void setLinkMethod(L linkMethod)
    {
        this.linkMethod = linkMethod;
    }

}
